package com.lemon.smartsafety.ui.adapter.fire;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lemon.smartsafety.R;
import com.lemon.smartsafety.result.fire.DeviceSResult;
import com.lemon.smartsafety.ui.activity.fire.FirefightingUtil;
import com.lemon.smartsafety.utils.CommonUtil;
import com.lemon.smartsafety.utils.LogUtils;
import com.lemon.smartsafety.view.AutoWrapLinearLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 项目名称：workspace
 * 类描述：
 * 创建人：Fargo
 * 创建时间：2016/9/8 10:04
 * 修改人：Fargo
 * 修改时间：2016/9/8 10:04
 * 修改备注：
 */
public class FireSituationDeviceAdapter extends BaseAdapter {

    /**
     * TAG
     */
    private final String TAG = "FireSituationDeviceAdapter";

    /**
     * 选择项
     */
    private int m_nSelectItem = -1;

    /**
     * 数据列表
     */
    private List<DeviceSResult> mList;


    /**
     * 布局泵
     */
    private LayoutInflater m_layInflater = null;

    private Context mContext;

    private TextView tagView;

    private AutoWrapLinearLayout autoWrapLinearLayout;

    private int mTAGgetWidth = 0;


    //是否显示右箭头
    private boolean isShowArrowRight = true;


    public boolean isShowArrowRight() {
        return isShowArrowRight;
    }

    public void setShowArrowRight(boolean showArrowRight) {
        isShowArrowRight = showArrowRight;
    }


    /**
     * 设置选择项
     *
     * @param n
     */
    public void setSelectItem(final int n) {
        m_nSelectItem = n;
    }

    /**
     * 设置数据列表
     */
    public void setList(ArrayList<DeviceSResult> list) {
        this.mList = list;
    }

    public void setmTAGgetWidth(int mTAGgetWidth) {
        this.mTAGgetWidth = mTAGgetWidth;
    }

    public int getmTAGgetWidth() {
        return mTAGgetWidth;
    }

    /**
     * 构造函数
     *
     * @param context
     */
    public FireSituationDeviceAdapter(Context context, List<DeviceSResult> list) {
        try {
            this.mContext = context;

            this.mList = list;

            // 布局泵
            m_layInflater = LayoutInflater.from(context);

        } catch (Exception e) {
        }
    }

    @Override
    public int getCount() {
        try {
            if (mList != null)
                return mList.size();
        } catch (Exception e) {
        }

        return 0;
    }

    @Override
    public Object getItem(int position) {
        try {
            if (mList != null)
                return mList.get(position);
        } catch (Exception e) {
        }

        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressWarnings({"deprecation"})
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        try {

            final ViewHolder holder;
            if (convertView == null) {
                convertView = m_layInflater.inflate(R.layout.item_device_situation, null);
                holder = new ViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }


            try {
                LogUtils.LOGE("加载TAG_VIEW" + position);
                holder.txtTitle.setText(mList.get(position).getObjectTitle());
                /* if (mList.get(position).getStatus().equals("1")) {
                    holder.txtContent1.setText("符合");
                } else if (mList.get(position).getStatus().equals("2")) {
                    holder.txtContent1.setText("不符合");
                } else {
                    holder.txtContent1.setText("无");
                }
                */
                if (!CommonUtil.strIsEmpty(mList.get(position).getRemark())) {
                    holder.txtSub.setVisibility(View.VISIBLE);
                    holder.txtSub.setText("备注:  " + mList.get(position).getRemark());
                } else {
                    holder.txtSub.setVisibility(View.GONE);
                }

                if (mList.get(position).getCheckValidity() != -1 && !CommonUtil.strIsEmpty(mList.get(position).getUnitName())) {
                    holder.txtCycle.setVisibility(View.VISIBLE);
                    holder.txtCycle.setText("检查周期:  " + mList.get(position).getCheckValidity() + mList.get(position).getUnitName());
                } else {
                    holder.txtCycle.setVisibility(View.GONE);
                }

                if (!CommonUtil.strIsEmpty(mList.get(position).getPosition())) {
                    holder.txtDirection.setVisibility(View.VISIBLE);
                    holder.txtDirection.setText("方位信息:  " + mList.get(position).getPosition());
                } else {
                    holder.txtDirection.setVisibility(View.GONE);
                }


                holder.txtLastInspect.setText("上次检查:  " + mList.get(position).getLastCheckUserName() + "  " + mList.get(position).getLastCheckDate());

                if (mTAGgetWidth == 0) {
                    mTAGgetWidth = holder.lineTag.getWidth();
                }

                holder.autoWrapLinearLayout.removeAllViews();
                if (mTAGgetWidth != 0) {
                    holder.autoWrapLinearLayout.setmWidth(mTAGgetWidth);
                }
                if (!CommonUtil.listIsEmpty(mList.get(position).getCheckRemark())) {
                    for (int i = 0; i < mList.get(position).getCheckRemark().size(); i++) {
                        tagView = (TextView) m_layInflater.inflate(R.layout.tag, holder.autoWrapLinearLayout, false);
                        tagView.setText(mList.get(position).getCheckRemark().get(i).getContent());
                        try {
                            tagView.setTextColor(ContextCompat.getColor(mContext,FirefightingUtil.getScoreTextColor(mList.get(position).getCheckRemark().get(i).getMarkCode())));
                            tagView.setBackgroundResource(FirefightingUtil.getScoreTextBgColor(mList.get(position).getCheckRemark().get(i).getMarkCode()));
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

            /*            if (mList.get(position).getCheckRemark().get(i).contains("不符合") || mList.get(position).getCheckRemark().get(i).contains("未检查")) {
                            tagView.setTextColor(ContextCompat.getColor(mContext, R.color.colorAppRed));
                            tagView.setBackgroundResource(R.drawable.shape_round_line_red);
                        } else {
                            tagView.setTextColor(ContextCompat.getColor(mContext, R.color.colorAppGreen));
                            tagView.setBackgroundResource(R.drawable.shape_round_line_green);
                        }*/
                        holder.autoWrapLinearLayout.addView(tagView);
                    }
                }


                //holder.autoWrapLinearLayout.invalidate();


                holder.imageCircle.setBackgroundResource(FirefightingUtil.getScoreImgCircleColor(mList.get(position).getStatus()));

            } catch (Exception e) {
                e.printStackTrace();
            }


        } catch (Exception e) {
            e.printStackTrace();
        }

        return convertView;
    }

    static class ViewHolder {
        @Bind(R.id.txt_title)
        TextView txtTitle;
        @Bind(R.id.txt_sub)
        TextView txtSub;
        @Bind(R.id.image_circle)
        ImageView imageCircle;
        @Bind(R.id.txt_cycle)
        TextView txtCycle;
        @Bind(R.id.txt_direction)
        TextView txtDirection;
        @Bind(R.id.txt_last_inspect)
        TextView txtLastInspect;
        @Bind(R.id.txt_content1)
        TextView txtContent1;
        @Bind(R.id.autoWrapLinearLayout)
        AutoWrapLinearLayout autoWrapLinearLayout;
        @Bind(R.id.lineTag)
        LinearLayout lineTag;
        @Bind(R.id.lineAll)
        LinearLayout lineAll;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }


    /**
     * 项支持器类
     */


}